home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / task0.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  6KB  |  221 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: task0.c,v 1.5 1997/07/09 13:26:13 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    Filename:    task0.c
  35.  *
  36.  * This program will place a "well known" service into a mailbox
  37.  * along with a communication context.  It will then wait for another
  38.  * task to communicate via that context.
  39.  *
  40.  * task1.c is designed as the "other" communicating task.
  41.  * task_end.c tells task0 to terminate itself and quit waiting.
  42.  *
  43.  * Note that you may load a number of these into the mailbox providing
  44.  * they have unique <service_name> key.
  45.  *
  46.  * The task_end.c program will cause this service to gracefully exit
  47.  * pvm.
  48.  *
  49.  *    usage: task0 [service_name] <flags>
  50.  *
  51.  *                PvmMboxDefault        d
  52.  *                PvmMboxPersistent    p
  53.  *                PvmMboxMultiInstance    m
  54.  *                PvmMboxOverWritable    o
  55.  *
  56.  *  files used:  task0.c taskf.c
  57.  */
  58.  
  59. #include <stdio.h>
  60. #ifndef WIN32
  61. #include <unistd.h>        /* for gethostname */
  62. #else
  63. #include "pvmwin.h"
  64. #endif
  65. #include <string.h>
  66. #include "pvm3.h"
  67.  
  68. main( argc, argv )
  69. int argc;
  70. char *argv[];
  71. {
  72.     char *me = "task0";
  73.     char machine[25];
  74.     int mytid;
  75.     int context0, context1;
  76.     char *service_name;
  77.     int service_ctr = 0;    /* count of the # of service requests */
  78.     int i;
  79.     int index;                /* returned index for mailbox entry */
  80.  
  81.     char flagc[10];            /* hold characters for flags */
  82.     int flags;                /* PvmMbox flags set */
  83.     char *flagstring;        /* pointer to string of flag settings */
  84.  
  85.     int their_tid;            /* tid of task sending service request */
  86.     char msg_txt[100];        /* message received as service request */
  87.     int msg_buf;            /* message buffer to receive on */
  88.  
  89.     /* display_incomming_parameters( me, argc, argv ); */
  90.  
  91.     /*
  92.      *  validate input parameters
  93.      */
  94.     if ( argc == 3 ) {
  95.         /* we got what we wanted -- all three */
  96.         strcpy( flagc, argv[2] );        /* copy flags to local */
  97.     }
  98.     else {
  99.         if ( argc == 2 ) {
  100.             /* assume flags left off */
  101.             /*   - so force flags to PvmMboxDefault */
  102.             strcpy( flagc, "d" );
  103.         }
  104.         else {
  105.             /* unknown input sequence - complain and exit */
  106.             printf( "\n\nusage: task0 [service_name] <flags>\n" );
  107.             printf( "\tPvmMboxDefault\t\td\n" );
  108.             printf( "\tPvmMboxPersistent\tp\n" );
  109.             printf( "\tPvmMboxMultiInstance\tm\n" );
  110.             printf( "\tPvmMboxOverWritable\to\n" );
  111.             exit( -1 );
  112.         }
  113.     }
  114.  
  115.     printf( "%s: sending <%s> to set_flags.\n", me, flagc );
  116.     if ( ( flags = set_flags( &flagc, &flagstring ) ) < 0 ) {
  117.         exit( -1 );    /* something failed - abort! */
  118.     }
  119.  
  120.     service_name = argv[1];
  121.     printf( "%s: service name <%s> with flags <%d> : <%s>.\n",
  122.             me, service_name, flags, flagstring );
  123.  
  124.     if ( ( mytid = pvm_mytid() ) == PvmSysErr ) {
  125.         printf( "\nPVM not up!\n" );
  126.         exit( -1 );
  127.     }
  128.  
  129.     gethostname( machine, 25 );
  130.  
  131.     printf( "%s: t%x on machine <%s> with context %d.\n",
  132.             me, mytid, machine, pvm_getcontext() );
  133.  
  134.     context1 = pvm_newcontext();            /* create new context1 */
  135.     context0 = pvm_setcontext( context1 );    /* activate new context1 */
  136.  
  137.     printf( "%s: t%x on machine <%s> with context %d.\n",
  138.             me, mytid, machine, pvm_getcontext() );
  139.  
  140.     /*
  141.      *  create message to place in mailbox
  142.      */
  143.     pvm_initsend( PvmDataDefault );
  144.     pvm_packf( "%+ %d %d %s",
  145.             PvmDataDefault, mytid, context1, machine );
  146.     if ( (index = pvm_putinfo( service_name, pvm_getsbuf(), flags ))
  147.             < 0 ) {
  148.         /*
  149.          *  did not find mailbox - tell user why and exit...
  150.          */
  151.         switch ( index ) {
  152.             case PvmExists:
  153.                 printf( "\n%s: Names service already running\n", me );
  154.                 break;
  155.             case PvmNotFound:
  156.                 printf( "\n%s: no service running\n", me );
  157.                 break;
  158.             case PvmBadParam:
  159.                 printf( "\n%s: Invalid argument to pvm_recvinfo().\n",
  160.                         me );
  161.                 break;
  162.             case PvmNoSuchBuf:
  163.                 printf( "\n%s: Message buffer id does not exist.\n",
  164.                         me );
  165.                 break;
  166.             case PvmDenied:
  167.                 printf(
  168.                     "\n%s: Key locked by another task, can't delete.\n",
  169.                         me );
  170.                 break;
  171.             default:
  172.                 lpvmerr( "\n%s: task0.c: error %d", me, index );
  173.                 break;
  174.         } /* end_switch */
  175.         exit( -1 );
  176.     }
  177.  
  178.     printf( "%s: just inserted service name <%s> at index %d.\n",
  179.             me, service_name, index );
  180.  
  181.     for ( ; ; )
  182.     {
  183.         /*
  184.          *  wait for service requests
  185.          */
  186.         printf( "%s: t%x on machine <%s> with context %d ",
  187.                 me, mytid, machine, pvm_getcontext() );
  188.         printf( "- waiting for service request.\n" );
  189.         msg_buf = pvm_recv( -1, -1 );
  190.         pvm_bufinfo( msg_buf, (int *) 0, (int *) 0, &their_tid );
  191.         pvm_upkstr( msg_txt );
  192.         printf( "%s: t%x %s  Request number %d.\n",
  193.                 me, mytid, msg_txt, ++service_ctr );
  194.  
  195.         if ( strncmp( msg_txt, "END", 3 ) == 0 )
  196.             break;
  197.  
  198.         /*
  199.          *  send a greeting message to service requestor
  200.          */
  201.         sprintf( msg_txt,
  202.             "welcome to my server - you are service request number %d.",
  203.                 service_ctr );
  204.         pvm_initsend( PvmDataDefault );
  205.         pvm_pkstr( msg_txt );
  206.         pvm_send( their_tid, 1 );
  207.     }
  208.  
  209.     /*
  210.      *  reset back to my original context
  211.      */
  212.     (void) pvm_setcontext( context0 );
  213.     printf( "%s: t%x on machine <%s> with context %d.\n",
  214.             me, mytid, machine, pvm_getcontext() );
  215.     printf( "%s: handled %d service requests.\n", me, service_ctr );
  216.  
  217.     pvm_exit();
  218.     exit( 0 );
  219. }
  220.  
  221.